package com.aidansmeaton.weightmanager;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aidansmeaton.weightmanager.adapter.DiaryFoodAdapter;
import com.aidansmeaton.weightmanager.data.DatabaseHelper;
import com.aidansmeaton.weightmanager.util.CalorieHelper;

/** The first activity in the app. */
public class DiaryFoodActivity extends ListActivity implements OnItemClickListener {
	
	private DatabaseHelper db;
	private String date;	

	private int goalCalories;
	private int cumulativeCalories;
	
	private ProgressBar progress;
	private TextView used;
	private TextView remaining;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_food);
        
        db = DatabaseHelper.getInstance(this);
    	date = this.getIntent().getStringExtra("Date");
        
        ListView list = getListView();
        list.setOnItemClickListener(DiaryFoodActivity.this);
        
	}
    
    @Override 
    public void onResume(){
    	super.onResume();
    	                
        progress = (ProgressBar) findViewById(R.id.calorie_progress);
        used = (TextView) findViewById(R.id.food_used);
        remaining = (TextView) findViewById(R.id.food_remaining);

        used.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));
        remaining.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_down));

    	// initialise consumed and goal calories
        cumulativeCalories = 0;
    	goalCalories = -1;
    	
    	// calculate goal for this day
    	Cursor weightCursor = db.getWeightLogsBeforeDate(date);
    	if (weightCursor.getCount() > 0){
    		weightCursor.moveToFirst();
    		int age = getSharedPreferences("Profile",0).getInt("Age",-1);
    		int weight = weightCursor.getInt(DatabaseHelper.WEIGHT_LOG_WEIGHT);	// get most recent weight log
    		String units = weightCursor.getString(DatabaseHelper.WEIGHT_LOG_COMMENT);
    		boolean imperial = units.equals("i");
    		boolean isFemale = getSharedPreferences("Profile", 0).getBoolean("Gender",true);
    		int exercise_selection = getSharedPreferences("Profile",0).getInt("Exercise", 0);
    		goalCalories = CalorieHelper.calculateRecommendedCaloriesPlan(age, weight, isFemale, exercise_selection, imperial);
    	}
    	weightCursor.close();
        
        if (!profileIsSetUp()){
        	progress.setVisibility(View.GONE);
        	remaining.setVisibility(View.GONE);
        	findViewById(R.id.goal_icon).setVisibility(View.GONE);
        	findViewById(R.id.food_remaining_title).setVisibility(View.GONE);
        }
        else {
        	progress.setMax(goalCalories);
        }
        
        Cursor allPlateCursor = db.getPlateLogsFromDate(date);
        allPlateCursor.moveToFirst();
        while (!allPlateCursor.isAfterLast()){
        	cumulativeCalories += allPlateCursor.getInt(DatabaseHelper.PLATE_LOG_CALORIES);
        	if (allPlateCursor.getInt(DatabaseHelper.PLATE_LOG_CALORIES) == 0){
        		cumulativeCalories += CalorieHelper.getCaloriesFromGrade(allPlateCursor.getInt(DatabaseHelper.PLATE_LOG_GRADE));
			}
        	allPlateCursor.moveToNext();
        }
        allPlateCursor.close();
		
        progress.setProgress(cumulativeCalories);
        used.setText(""+cumulativeCalories);
        remaining.setText(""+(goalCalories-cumulativeCalories));
    	
    	Cursor c = db.getFoodLogsForDate(date);
        getListView().setAdapter(new DiaryFoodAdapter(this, c));
    	
    }
    
    public void recordFoodClick(View v){
    	Intent i = new Intent(this, DiaryFoodAddActivity.class);
	    i.putExtra("Date", date);
	    startActivity(i);
    }
    
    public void recordFavouriteClick(View v){
    	Intent i = new Intent(this, DiaryFoodFavouritesActivity.class);
	    i.putExtra("Date", date);
	    startActivity(i);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    	Intent i = new Intent(this, DiaryFoodAddActivity.class);
	    i.putExtra("Date", date);
	    i.putExtra("Food", (int)(Integer)view.getTag(R.id.food_row));
	    startActivity(i);
    }
    
    public boolean profileIsSetUp(){
    	return (goalCalories != -1);
    }
	
}